<?php

namespace app\api\controller;

use app\api\BaseController;
use app\model\TaskBackImgModel;
use think\facade\Config;
use think\facade\Filesystem;

class TaskBackImg extends BaseController
{
    public function upload()
    {
        $params = $this->request->param();

        $file = $this->request->file('image');
        $saveName = Filesystem::disk('public')->putFile('img',$file);

        $config = Config::get('filesystem');

        $taskBackImgModel = TaskBackImgModel::create([
            'img_url' => $config['disks']['public']['url'].'/'.$saveName,
            'host' => $this->request->domain(),
            'task_id' => $params['taskId']
        ]);

        return show($taskBackImgModel);

    }

    public function del()
    {
        $params = $this->request->param();

        if (empty($params['id'])) {
            return show([],1,'缺少参数id');
        }
        $taskBackImgModel = TaskBackImgModel::find($params['id']);

        if ($taskBackImgModel === null) {
            return show([],1,'已删除');
        }

        $taskBackImgModel->delete();

        return show();

    }

}